package com.atguigu.srb.core.controller.admin;

import com.atguigu.common.exception.Assert;
import com.atguigu.common.result.R;
import com.atguigu.common.result.ResponseEnum;
import com.atguigu.srb.core.pojo.entity.IntegralGrade;
import com.atguigu.srb.core.service.IntegralGradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
@Slf4j
//@CrossOrigin
@RestController
@RequestMapping("/admin/core/integralGrade")
@Api(tags = "积分等级管理")
public class AdminIntegralGradeController {
    @Resource
    private IntegralGradeService integralGradeService;

    @GetMapping("/list")
    @ApiOperation("积分等级列表")
    public R listAll(){

        log.info("日志信息");
        log.warn("警告日志");
        log.error("错误日志");

        List<IntegralGrade> list = integralGradeService.list();
        return R.ok().data("list",list).message("获取列表成功");
    }
    @DeleteMapping("/remove/{id}")
    @ApiOperation(value="根据id删除积分等级",notes = "逻辑删除数据记录")
    public R removeById(
            @ApiParam(value = "数据id",example = "100",required = true)
            @PathVariable Long id){
        boolean result = integralGradeService.removeById(id);
        if(result){
            return R.ok().message("删除成功");
        }else{
            return R.error().message("删除失败");
        }
    }
    @ApiOperation("新增积分等级")
    @PostMapping("/save")
    public R save(
            @ApiParam(value = "积分等级对象",required = true)
            @RequestBody IntegralGrade integralGrade){
        //if(integralGrade.getBorrowAmount()==null){
        //    throw new BusinessException(ResponseEnum.BORROW_AMOUNT_NULL_ERROR);
        //}
        Assert.notNull(integralGrade.getBorrowAmount(), ResponseEnum.BORROW_AMOUNT_NULL_ERROR);
        boolean result = integralGradeService.save(integralGrade);
        if(result){
            return R.ok().message("积分等级保存成功");
        }else{
            return R.error().message("积分等级保存失败");
        }
    }
    @ApiOperation(value = "根据积分ID查询积分")
    @GetMapping("/get/{id}")
    public R getById(
            @ApiParam(value = "积分ID",example = "100",required = true)
            @PathVariable Long id
    ){
        IntegralGrade integralGrade= integralGradeService.getById(id);
        if(integralGrade!=null){
            return R.ok().data("recore",integralGrade).message("成功查询积分等级");
        }else{
            return R.error().message("没有查询结果");
        }
    }
    @ApiOperation(value = "修改积分等级")
    @PutMapping("/update")
    public R updateById(
            @ApiParam(value = "修改积分等级",required = true)
            @RequestBody IntegralGrade integralGrade
    ){
        boolean result = integralGradeService.updateById(integralGrade);
        if(result){
            return R.ok().message("积分等级修改成功");
        }else{
            return R.error().message("积分等级修改失败");
        }
    }
}
